<?php
namespace Home\Controller;
use Think\Controller;
use App\Controller\BaseController;
class MrmidiController extends BaseController {

    public function _initialize(){
        //php判断客户端是否为手机
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS"))
        header("Location:http://xuancode.com/midi/index.php/Web/");
        //如果session存在，传递用户信息
        $midiuser = session('midiuser');
        $this->assign('midiuser',$midiuser);
    }

    /**
     * search（发现）
     * @author xuan
     * @param city          城市id（选传）
     * @param sex           迷笛先生性别 1男 2女（选传）
     * @param major_id      迷笛先生专业 1电吉他 2爵士鼓 3电贝司（选传）
     * @param level         迷笛先生等级 1初级 2中级 3高级（选传）
     */
    public function index(){
    	$json_arr = array_filter($_GET);

        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;

    	$data_sir['user_role'] = array('IN','2,3');
        $data_sir['user_display'] = 1;
        $data_sir['mrmidi_auth_status'] = 2;
        if (!empty($json_arr['city'])) {
            $data_sir['user_city'] = $json_arr['city'];
        }
        if (!empty($json_arr['sex'])) {
            $data_sir['user_sex'] = $json_arr['sex'];
        }
        if (!empty($json_arr['major_id'])) {
            $data_sir['mrmidi_major_id'] = $json_arr['major_id'];
        }
        if (!empty($json_arr['level'])) {
            $data_sir['mrmidi_level'] = $json_arr['level'];
        }
        $mrmidi_arr = M('mrmidi')->alias('m')->join('left join midi_user as u on m.mrmidi_user_id = u.user_id')->join('left join midi_city as c on u.user_city = c.city_id')->field('u.user_id,u.user_avatar,u.user_name,u.user_sex,u.user_phone,u.user_orgname,u.user_weixin,u.user_introduction,c.city_name')->where($data_sir)->page($p,$pnum)->group('u.user_id')->select();
        $count = M('mrmidi')->alias('m')->join('left join midi_user as u on m.mrmidi_user_id = u.user_id')->join('left join midi_city as c on u.user_city = c.city_id')->field('u.user_id,u.user_avatar,u.user_name,u.user_sex,u.user_phone,u.user_orgname,u.user_weixin,u.user_introduction,c.city_name')->where($data_sir)->group('u.user_id')->select();
        $c = count($count);
        $page = getpage($c,10);
        if (empty($mrmidi_arr)) {
            $mrmidi_arr = array();
        }else{
            foreach ($mrmidi_arr as $k => $v) {
                $mr['mrmidi_user_id'] = $v['user_id'];
                $mr['mrmidi_auth_status'] = 2;
                $mrmidi = M('mrmidi')->field('mrmidi_major_id,mrmidi_level')->where($mr)->select();
                foreach ($mrmidi as $key => $value) {
                    if ($value['mrmidi_major_id'] == 1) {
                    $mrmidi_arr[$k]['mrmidi_guitar_level'] = $value['mrmidi_level'];
                    }elseif ($value['mrmidi_major_id'] == 2) {
                        $mrmidi_arr[$k]['mrmidi_drum_level'] = $value['mrmidi_level'];
                    }elseif ($value['mrmidi_major_id'] == 3) {
                        $mrmidi_arr[$k]['mrmidi_bass_level'] = $value['mrmidi_level'];
                    }
                }
            }
        }
        // echo '<pre>';
        // print_r($mrmidi_arr);
        // exit();
        $this->assign('mrmidi_arr',$mrmidi_arr);
        //城市列表
        $sql = "select * from midi_city order by CONVERT( city_name USING gbk ) COLLATE gbk_chinese_ci ASC";
        $city_arr = M()->query($sql);
        $this->assign('city_arr',$city_arr);
        $this->assign('page', $page->show());
        $this->display();
    }
}